package qingxin.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import qingxin.dao.VoteOptionDAO;
import qingxin.dbc.DatabaseConnection;
import qingxin.vo.VoteOption;

public class VoteOptionDAOImpl implements VoteOptionDAO {

	@Override
	public VoteOption getByID(int voteOptionID) throws Exception{
		VoteOption voteOption = new VoteOption();
		String sql = "select void, vo_vid, `option`, number from vote_option where void = ?";
		DatabaseConnection dbc = null;
		try{
			dbc = new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql);
			
			pstmt.setInt(1, voteOptionID);
			
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				voteOption.setVoteOptionID(rs.getInt(1));
				voteOption.setVoteID(rs.getInt(2));
				voteOption.setOption(rs.getString(3));
				voteOption.setNumber(rs.getInt(4));
			}
		}
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
		return voteOption;
	}

	@Override
	public VoteOption[] getByVoteID(int voteQuestionID) throws Exception{
		ArrayList<VoteOption> voteOptions = new ArrayList<VoteOption>();
		String sql = "select void, vo_vid, `option`, number from vote_option where vo_vid = ?";
		DatabaseConnection dbc = null;
		try{
			dbc = new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql);
			
			pstmt.setInt(1, voteQuestionID);
			
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				VoteOption voteOption = new VoteOption();
				voteOption.setVoteOptionID(rs.getInt(1));
				voteOption.setVoteID(rs.getInt(2));
				voteOption.setOption(rs.getString(3));
				voteOption.setNumber(rs.getInt(4));
				voteOptions.add(voteOption);
			}
		}
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
		return voteOptions.toArray(new VoteOption[0]);
	}

	@Override
	public void addVoteOption(VoteOption voteOption) throws Exception {
		String sql = "insert into vote_option (vo_vid, `option`) values(?,?)";
		DatabaseConnection dbc = null;
		try{
			dbc = new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			pstmt.setInt(1, voteOption.getVoteID());
			pstmt.setString(2, voteOption.getOption());
			
			pstmt.executeUpdate();
			
			ResultSet key = pstmt.getGeneratedKeys();
			if(key.next()){
				voteOption.setVoteOptionID(key.getInt(1));
			}
		}
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
	}

	@Override
	public void addNumber(int voteOptionID) throws Exception {
		String sql ="update vote_option set number=number+1 where void=?";
		DatabaseConnection dbc = new DatabaseConnection();
		try{
			dbc = new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			pstmt.setInt(1, voteOptionID);
			
			pstmt.executeUpdate();
		}
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
		
	}

}
